package com.project.petLossSystem.service.impl;

import com.project.petLossSystem.common.Constants;
import com.project.petLossSystem.common.ServiceResultEnum;
import com.project.petLossSystem.mapper.UserMapper;
import com.project.petLossSystem.po.User;
import com.project.petLossSystem.service.UserService;
import com.project.petLossSystem.util.MD5Util;
import com.project.petLossSystem.util.MyBeanUtil;
import com.project.petLossSystem.util.PageQueryUtil;
import com.project.petLossSystem.util.PageResult;
import com.project.petLossSystem.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String register(String loginId, String loginPasswordMd5) {
        // 查询是否存在已注册且未注销的手机号
        if (userMapper.getCountByLoginIdAndDeleted(loginId, (byte) 0) > 0) {
            return ServiceResultEnum.SAME_LOGIN_ID_EXIST.getResult();
        }
        // 需要生成默认昵称和头像
        int random = new Random().nextInt(5); // 0-4
        User user = new User();
        user.setLoginId(loginId);
        user.setLoginPassword(loginPasswordMd5);
        user.setUserName("游客" + random + loginId.substring(7)); // 例如：游客28009
        user.setProfileUrl(Constants.PROFILE_URL + "local_" + random + ".jpg"); // 默认随机头像
        user.setUserIntroduction(loginId); // 默认联系手机号为注册手机号
        if (userMapper.insertUser(user) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        } else return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String login(String loginId, String loginPasswordMd5, HttpSession session) {
        User user = userMapper.login(loginId, loginPasswordMd5);
        if (user == null) {
            // 不存在或者被注销了
            return ServiceResultEnum.LOGIN_ERROR.getResult();
        }
        if (user.getIsLocked() == 1) {
            // 被锁定
            return ServiceResultEnum.LOGIN_USER_LOCKED.getResult();
        }
        // 过滤敏感信息
        UserVO userVO = new UserVO();
        MyBeanUtil.copyProperties(user, userVO);
        // 存储session里
        session.setAttribute("user", userVO);
        return ServiceResultEnum.SUCCESS.getResult();
    }

    @Override
    public PageResult getUsersPage(PageQueryUtil pageQueryUtil) {
        List<User> userList = userMapper.userList(pageQueryUtil);
        long userCount = userMapper.userCount(pageQueryUtil);
        return new PageResult(userCount, pageQueryUtil.getRows(), pageQueryUtil.getPage(), userList);
    }

    @Override
    public boolean updateLock(Long[] userIds, Byte isLocked) {
        return userMapper.updateLockByIdsAndStatus(userIds, isLocked) > 0;
    }

    @Override
    public boolean updateDeleted(Long[] userIds, Byte isDeleted) {
        return userMapper.updateDeletedByIdsAndStatus(userIds, isDeleted) > 0;
    }

    @Override
    public UserVO getUserMessage(Long userId) {
        User user = userMapper.getUserById(userId);
        if(ObjectUtils.isEmpty(user)){
            return null;
        }
        UserVO userVO = new UserVO();
        MyBeanUtil.copyProperties(user, userVO);
        userVO.setLoginId(""); // 隐藏用户登录名
        return userVO;
    }

    @Override
    public boolean updateNameAndIntroduction(String userName, String userIntroduction, HttpSession session) {
        Long userId = ((UserVO) session.getAttribute("user")).getUserId();
        if (userMapper.updateNameAndIntroductionById(userId, userName, userIntroduction) > 0) {
            // 修改成功，重新设置session
            session.removeAttribute("user");
            User user = userMapper.getUserById(userId);
            UserVO userVO = new UserVO();
            MyBeanUtil.copyProperties(user, userVO);
            session.setAttribute("user", userVO);
            return true;
        }
        return false;
    }

    @Override
    public String updatePassword(String oldPassword, String newPassword, HttpSession session) {
        oldPassword = MD5Util.MD5Encode(oldPassword, "UTF-8");
        Long userId = ((UserVO) session.getAttribute("user")).getUserId();
        User user = userMapper.getUserById(userId);
        if (!user.getLoginPassword().equals(oldPassword)) {
            return ServiceResultEnum.PASSWORD_ERROR.getResult();
        }
        newPassword = MD5Util.MD5Encode(newPassword, "UTF-8");
        if(userMapper.updatePassword(userId, newPassword) > 0){
            return ServiceResultEnum.SUCCESS.getResult();
        }else return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public long getUserCountToday() {
        return userMapper.getCountToday();
    }

    @Override
    public boolean updateImg(String imgUrl, Long userId, HttpSession session) {
        if(userMapper.updateImgById(userId, imgUrl) > 0){
            // 修改成功，重新设置session
            session.removeAttribute("user");
            User user = userMapper.getUserById(userId);
            UserVO userVO = new UserVO();
            MyBeanUtil.copyProperties(user, userVO);
            session.setAttribute("user", userVO);
            return true;
        }
        return false;
    }
}
